﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace UI.Forms
{
    public partial class ProductSale_Check : FormBase1
    {

        public ProductSale_Check()
        {
            InitializeComponent();
        }

        private void ProductSale_Check_Load(object sender, EventArgs e)
        {
            Bind();
        }

        /// <summary>
        /// 绑定未审核销售单
        /// </summary>
        private void Bind()
        {
            BindingList<SVC.client.View_ProductSale_M> ds = my.bw.NoCheckProductSale_Get(my.ACID);
            gridControl1.DataSource = ds;
            if (ds.Count > 0)
            {
                //因为默认第一条销售单选中 所以加载第一条销售单的明细
                Bind_D(gridView1.GetRowCellValue(0, gridColumn1).ToString()); 
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                string djn=gridView1.GetRowCellValue(e.RowHandle, gridColumn1).ToString();
                Bind_D(djn);
            }
            catch (Exception ex)
            {
                Comm.ShowErorr(ex.Message);
            }
        }

        /// <summary>
        /// 绑定销售单明细
        /// </summary>
        private void Bind_D(string djn)
        {
            BindingList<SVC.client.View_ProductSale_D> ds = my.bw.Get_ProductSale_D_By_M(djn,my.ACID);
            gridControl2.DataSource = ds;
        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Bind();
        }

        //通过选中
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CheckMethod("已通过");
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CheckMethod("已拒绝");
        }

        /// <summary>
        /// 执行审批方法
        /// </summary>
        /// <param name="status"></param>
        private void CheckMethod(string status)
        {
            if (gridView1.SelectedRowsCount == 0)
            {
                Comm.ShowAlert("请先选中要审核的销售单！");
                return;
            }
            string djn = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, gridColumn1).ToString();
            if (XtraMessageBox.Show("您确定要对销售单【" + djn + "】执行【" + status + "】操作？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                //先得到选中的
                SVC.client.ProductSale_M m = new SVC.client.ProductSale_M();
                m.djn = djn;
                m.checkstatus = status;
                string jg = my.bw.CheckProductSale(m, my.ACID);
                if (jg == "")
                {
                    Comm.ShowAlert("销售单【" + djn + "】" + status + "");
                    Bind();
                }
                else
                {
                    Comm.ShowAlert(jg);
                }
            }
        }

        
    }
}
